//
//  RHDeviceListCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/25.
//

#import "RHDeviceListCell.h"
#import "RHTools.h"

@interface RHDeviceListCell()

@property (nonatomic ,strong) UIImageView *markV;

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUIButton *unBindBtn;

@property (nonatomic ,strong) QMUIButton *editBtn;

@property (nonatomic ,strong) NSMutableArray *contentLabArr;

@property (nonatomic ,strong) QMUILabel *createTimeLab;

@property (nonatomic ,strong) QMUILabel *devTypeLab;//设备类型

@property (nonatomic ,strong) QMUILabel *plantNameLab;//电站

@property (nonatomic ,strong) QMUILabel *snLab;//sn

@property (nonatomic ,strong) QMUILabel *firmwareLab;//固件
@end

@implementation RHDeviceListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}
-(void)setDevice:(RHDeviceModel *)device
{
    _device = device;
    self.titleLab.text = device.deviceName;
   //设备类型
    self.devTypeLab.text = [NSString stringWithFormat:@"%@-%@",ValidNullData(device.productTypeName),ValidNullData(device.deviceTypeName)];
    
    //电站名称
    self.plantNameLab.text = ValidNullDataToString(device.plantName, @"--");
    
    //SN
    self.snLab.text = device.deviceId;

    //软件版本
    self.firmwareLab.text = ValidNullDataToString(device.deviceSoftware, @"--");
    
    //安装时间
    NSDate *installDate = [NSDate dateWithTimeIntervalSince1970:device.installTime/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    self.createTimeLab.text = [formatter stringFromDate:installDate];
    
    if(device.installTime == 0 || device.installTime == kCFNull){
        self.createTimeLab.text = @"--";
    }
    
    //在线状态
    if([device.status isEqualToString:@"1"]){
        //离线
        _markV.backgroundColor = kCOLOR_HEX(@"BBBBBB");
    }else if([device.status isEqualToString:@"0"]){
        //在线
        _markV.backgroundColor = CMainColorGreen;
    }
    
    //故障状态
    if(device.normal == 2){
        //故障
        _markV.backgroundColor = kCOLOR_HEX(@"FA6334");
    }else if(device.normal == 1){
        //告警
        _markV.backgroundColor = kRGB(251, 159, 41);
    }
   
}
-(void)setCanDelete:(BOOL)canDelete{
    self.unBindBtn.hidden = !canDelete;
}

-(void)hideEditButton
{
    self.editBtn.hidden = YES;
    [self.editBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(0);
    }];
}

-(void)setModifyBlock:(void (^)(void))modifyBlock
{
    _modifyBlock = modifyBlock;
}

- (void)setupUI{
    kWeakSelf;
    _contentLabArr = [NSMutableArray array];
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView rounded:8 width:1 color:kBorderColor];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    
    _markV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
    [_markV.layer setCornerRadius:4];
    [bgView addSubview:_markV];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setFont:[UIFont systemFontOfSize:14]];
    [_titleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    _titleLab.adjustsFontSizeToFitWidth = YES;
    _titleLab.minimumScaleFactor = 0.8;
    [bgView addSubview:_titleLab];
    
    _createTimeLab = [[QMUILabel alloc]init];
    _createTimeLab.textAlignment = NSTextAlignmentRight;
    _createTimeLab.font = kSYSTEMFONT(11);
    _createTimeLab.textColor = kCOLOR_HEX(@"AAAAAA");
    [bgView addSubview:_createTimeLab];
    _createTimeLab.hidden = YES;
    
    UIView *spaceView = kView;
    [bgView addSubview:spaceView];
    [spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(1);
        make.right.equalTo(weakSelf.createTimeLab.mas_left);
        make.centerY.equalTo(weakSelf.markV);
        make.width.greaterThanOrEqualTo(@(1));
    }];
    
    _editBtn = [QMUIButton buttonWithImage:@"plant_edit"];
    [bgView addSubview:_editBtn];
    [_editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(30);
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
        make.right.equalTo(spaceView.mas_left).offset(-10);
        make.centerY.equalTo(weakSelf.titleLab);
    }];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];

  
    //-------------------------
    
    QMUILabel *devTypeTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    devTypeTitleLab.text = [NSString stringWithFormat:@"%@:",@"设备类型".rh_localized];
    [bgView addSubview:devTypeTitleLab];
    [devTypeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(lineV.mas_bottom).offset(15);
    }];
    
    _devTypeLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
    _devTypeLab.text = @"--";
    [bgView addSubview:_devTypeLab];
    [_devTypeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineV);
        make.centerY.equalTo(devTypeTitleLab);
    }];
    
    QMUILabel *lastLabel;
    //电站进去的设备列表不显示电站名称   RHDeviceListCell_noPlantName
    if ([self.reuseIdentifier isEqualToString:@"RHDeviceListCell"]) {
        QMUILabel *plantTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
        plantTitleLab.text = [NSString stringWithFormat:@"%@:",@"电站名称".rh_localized];
        [bgView addSubview:plantTitleLab];
        [plantTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lineV);
            make.top.equalTo(devTypeTitleLab.mas_bottom).offset(15);
        }];
        
        _plantNameLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
        _plantNameLab.text = @"--";
        [bgView addSubview:_plantNameLab];
        [_plantNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(lineV);
            make.centerY.equalTo(plantTitleLab);
        }];
        lastLabel = plantTitleLab;
    }
    
    
    
    QMUILabel *snTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    snTitleLab.text = [NSString stringWithFormat:@"%@:",@"SN号".rh_localized];
    [bgView addSubview:snTitleLab];
    [snTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(lastLabel ? lastLabel.mas_bottom:devTypeTitleLab.mas_bottom).offset(15);
    }];
    
    _snLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
    _snLab.text = @"--";
    [bgView addSubview:_snLab];
    [_snLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineV);
        make.centerY.equalTo(snTitleLab);
    }];
    
    QMUILabel *firmwareTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    firmwareTitleLab.text = [NSString stringWithFormat:@"%@:",@"固件".rh_localized];
    [bgView addSubview:firmwareTitleLab];
    [firmwareTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(snTitleLab.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
    }];
    
    _firmwareLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentRight];
    _firmwareLab.text = @"--";
    [bgView addSubview:_firmwareLab];
    [_firmwareLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineV);
        make.centerY.equalTo(firmwareTitleLab);

    }];
    
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(10);
        make.bottom.mas_offset(-5);
    }];
    
    [_markV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(8, 8));
        make.left.mas_offset(15);
        make.top.mas_offset(16);
    }];
    
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.left.equalTo(weakSelf.markV.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.markV);
    }];
    
    [_createTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(weakSelf.titleLab);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.left.equalTo(weakSelf.markV);
        make.right.equalTo(weakSelf.createTimeLab);
        make.top.equalTo(weakSelf.markV.mas_bottom).offset(16);
    }];
    

    [_markV setBackgroundColor:CFMainColor];
 
    [[_editBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.modifyBlock) {
            weakSelf.modifyBlock();
        }
    }];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
